package cn.com.dhcc.ddi.node.service;

import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.pagerhandler.Pager;
import cn.com.dhcc.app.core.handler.pagerhandler.PagerData;
import cn.com.dhcc.app.core.service.BaseService;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.pub.CM;
import cn.com.dhcc.ddi.node.dao.DomainInfoDao;
import cn.com.dhcc.ddi.node.vo.DomainInfo;
import cn.com.dhcc.ddi.node.vo.wapper.DomainInfoRowWapper;
/**
 * 
    * @ClassName: DomainInfoService
    * @Description: 业务域Service
    * @author 18315
    * @date 2017-3-10
    *
 */
@Service
public class DomainInfoService extends BaseService {
	private static Logger logger = Log.getLogger(LOGTYPE.DDI);
	@Autowired
	private DomainInfoDao domainInfoDao;
	
	/**
	 * 获取业务域信息分页列表
	 * @param pager
	 * @param condition
	 * @return
	 * @throws ServiceException
	 */
	public PagerData<DomainInfo> list(Pager pager, WhereCondition condition) throws ServiceException {
		try {
			return domainInfoDao.list(pager, condition);
		} catch (DaoException e) {
			throw new ServiceException("获取业务域分页信息列表失败", e);
		}
	}
	
	/**
	 * 获取业务域信息列表
	 * @param pager
	 * @param condition
	 * @return
	 * @throws ServiceException
	 */
	public List<DomainInfo> list() throws ServiceException {
		try {
			return domainInfoDao.list();
		} catch (DaoException e) {
			throw new ServiceException("获取业务域信息列表失败", e);
		}
	}
	
	/**
	 * 
	 * 
	    * @Title: getDomainInfo
	    * @Description: 获取业务域
	    * @param @return
	    * @param @throws ServiceException    参数
	    * @return List<DomainInfo>    返回类型
	    * @throws
	 */
	public List<DomainInfo> getDomainInfo()
			throws ServiceException {
		try {
			return domainInfoDao.getDomainInfo();
		} catch (DaoException e) {
			throw new ServiceException("获取业务域id失败", e);
		}
	}
	
	/**
	 * 根据数据分类编码获取业务域实例
	 * @param 
	 * @return
	 * @throws ServiceException
	 */
	public DomainInfo getDomainInfo(String id) throws ServiceException{
		try {
			return domainInfoDao.getDomainInfo(id);
		} catch (DaoException e) {
			throw new ServiceException("获取列表失败", e);
		}
	}
	
	
	/**
	 * 
	    * @Title: getOrgCode
	    * @Description: 获取所有有效业务域信息
	    * @param @return
	    * @param @throws ServiceException    参数
	    * @return List<DomainInfo>    返回类型
	    * @throws
	 */
	public List<DomainInfo> getDomain()
			throws ServiceException {
		try {
			return domainInfoDao.getAllDomainList();
		} catch (DaoException e) {
			throw new ServiceException("获取失败", e);
		}
	}
	
	/**
	 * 
	    * @Title: insert
	    * @Description: 添加修改业务域信息
	    * @param @param vo
	    * @param @return
	    * @param @throws ServiceException    参数
	    * @return int    返回类型
	    * @throws
	 */
	public int insert(DomainInfo vo) throws ServiceException {
		
		try {
			return domainInfoDao.insert(vo);
		} catch (DaoException e)  {
			throw new ServiceException("新增失败", e);
		}
	}
	
    public int update(DomainInfo vo) throws ServiceException {
		
		try {
			return domainInfoDao.update(vo);
		}catch (DaoException e)  {
			throw new ServiceException("更新失败", e);
		}
	}
	/**
	 * 
	    * @Title: insertOrUpdate
	    * @Description: 修改或者更新
	    * @param @param obj
	    * @param @throws DaoException    参数
	    * @return void    返回类型
	    * @throws
	 */
	public int insertOrUpdate(DomainInfo obj) throws ServiceException {
		try{
			return domainInfoDao.insertOrUpdate(obj);
	} catch (DaoException e) {
		throw new ServiceException("更新任务失败", e);
	  }
	}
	/**
	 * 
	    * @Title: get
	    * @Description: 获取对象
	    * @param @param id
	    * @param @return
	    * @param @throws DaoException    参数
	    * @return NodeInfo    返回类型
	    * @throws
	 */
	public DomainInfo get(String id) throws ServiceException {
		try {
		  return domainInfoDao.get(id);
		} catch (DaoException e) {
			throw new ServiceException("根据id获取任务失败", e);
		}
		
	}
	
	/**
	 * 
	    * @Title: delete
	    * @Description: 删除
	    * @param @param status
	    * @param @param id
	    * @param @return    参数
	    * @return boolean    返回类型
	    * @throws
	 */
	public boolean delete(String id) {
		boolean result = false;
		String sql = "delete from " + SI_TABLE("t_ddi_domain_info") 
				+ " WHERE id=?";
		try {
			result = CM.getDao().delete("删除", sql, new Object[]{id})> 0 ? true : false;
		} catch (DaoException e) {
			new ServiceException("删除失败", e);
		}
		return result;
	}
	
	
	public List<DomainInfo> listdomainId(String id) throws DaoException {
		
		String sql = "SELECT a.id FROM " + SI_TABLE("t_ddi_domain_info") + " a " +","
		
				+ SI_TABLE("t_ddi_data_catagory") + " b "+ " where a.id=? and a.id = b.domain_id";
				
	
			return getDao().listModule("获取id列表",sql,DomainInfo.class,new Object[]{id});
		
	}
	
	
	/**
	 * 
	    * @Title: batchDelete
	    * @Description: 批量删除(备用)
	    * @param @param ids
	    * @param @return
	    * @param @throws DaoException
	    * @param @throws IllegalArgumentException    参数
	    * @return int    返回类型
	    * @throws
	 */
	
	public int batchDelete(String[] ids) throws DaoException,IllegalArgumentException {
               int num = 0;
				  try {
					 Assert.noNullElements(ids);
					 List<Object[]> params = new ArrayList<Object[]>();
					 for (String id : ids) {
						params.add(new Object[] { id });
					}
					String sql = "delete from  " + SI_TABLE("t_ddi_domain_info")
							+ " where id = ?";
					num = CM.getDao().batchDelete("批量删除", sql, params);
				} catch (DaoException e) {
					throw new DaoException("批量删除失败", e);
				}
				return num;
				}
	
	/**
	 * 验证名称唯一性
	 * @param name
	 * @param checkStr
	 * @return
	 * @throws ServiceException
	 */
	
	
	public boolean isPropertyUnique(String propertyName, String newValue, String oldValue) throws ServiceException {
		try {
			if (newValue.equals(oldValue)) {
				return true;
			}
			WhereCondition searchParameter = new WhereCondition();
			searchParameter.where().eq(propertyName, newValue);
			return domainInfoDao.count(searchParameter) == 0;
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 根据id停用domain
	 * @param domainInfo 
	 * @param status 状态 0：停用  1：启用
	 * @return true 成功   false 失败
	 * @throws DaoException 
	 */
	public boolean del(String id, String status) {
		boolean result = false;
		String sql = "UPDATE " + SI_TABLE("t_ddi_domain_info") + " SET status=?"
				+ " WHERE id=?";
		try {
			result = CM.getDao().update("更新domainInfo", sql, new Object[]{status, id}) > 0 ? true : false;
		} catch (DaoException e) {
			new ServiceException("更新domainInfo表失败", e);
		}
		return result;
	}
	
	/**
	 * 获取全部停用业务域
	 * 
	 * @return
	 * @throws DaoException
	 */
	public List<DomainInfo> getAllDomainInfoList() throws DaoException {
		try {
			final String sql = "select * from "
					+ SI_TABLE("t_ddi_node_info")
					+ " where  status <>'1'";
			return CM.getDao().list("获取全部停用业务域", sql, new DomainInfoRowWapper(),
					new Object[] {});
		} catch (DaoException e) {
			throw new DaoException("获取全部停用业务域失败！", e);
		}
	}
	
}
